package com.jacklei;
import com.jacklei.util.ArrUtil;

import java.util.Arrays;

//选择排序
public class SelectSort {
    public static void main(String[] args) {
       int[] arr = ArrUtil.getRandomArr();
       int[] arr1 = arr;
       int[] arr2 = ArrUtil.copyArr(arr);
        Arrays.sort(arr1);
        sort(arr2);
        for (int i = 0; i < arr1.length; i++) {
            if(arr1[i] != arr2[i])
                System.out.println("失败了arr :"+i+"=>arr1:"+arr1[i] +"=>arr2"+arr2[i] );
                break;
        }
        System.out.println("成功！！！");
        for (int i : arr) {
            System.out.print(i+"，");
        }
        System.out.println();
        for (int i : arr2) {
            System.out.print(i+"，");
        }
    }

    public static void sort(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = i; j <arr.length; j++ ){
                if (arr[i] > arr[j]) {
                    swap(arr, i, j);
                }
            }
        }
    }

    public static void swap(int[] arr, int a, int b){
        arr[a] = arr[a] ^ arr[b];
        arr[b] = arr[a] ^ arr[b];
        arr[a] = arr[a] ^ arr[b];
    }
}
